﻿//
// GNS_PvEAllInfoDaily.cpp
// MobileManage
//
// Created by MR.L on 16/2/18.
//
//
#include "GnsClientFrame.h"
#include "GNS_PvEAllInfoDaily.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_PvEAllInfoDaily::GNS_PvEAllInfoDaily()
{
    pGNS_PvEAllInfoDaily=0;
    DataListView=nullptr;
    TimeText=nullptr;
    pCalendar=nullptr;
    pProName=nullptr;
    GameType=nullptr;
    GameName=nullptr;
    PlayerName=0;
    CurID=0;
    Year=0;
    Mon=0;
    Day=0;
}

GNS_PvEAllInfoDaily::~GNS_PvEAllInfoDaily()
{
    
}

GNS_PvEAllInfoDaily* GNS_PvEAllInfoDaily::createLayer()
{
    auto pGNS_PvEAllInfoDaily = GNS_PvEAllInfoDaily::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PvEAllInfoDaily);
    
    return pGNS_PvEAllInfoDaily;
}

bool GNS_PvEAllInfoDaily::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;
    return true;
}
void GNS_PvEAllInfoDaily::SetNodeName(std::string CsbName)
{
    pGNS_PvEAllInfoDaily = CSLoader::createNode(CsbName);
    pGNS_PvEAllInfoDaily->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PvEAllInfoDaily);
    addChild(pGNS_PvEAllInfoDaily,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PvEAllInfoDaily);
    auto pBackBtn = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("PvPApplyTickBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
    
    DataListView = static_cast<ListView*>(pGNS_PvEAllInfoDaily->getChildByName("DateListView"));
    
    auto pTimeBtn = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("TimeBtn"));
    pTimeBtn->setTag(2);
	pTimeBtn->setVisible(false);
    //pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
    
    auto pTimeSer = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    //pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
	auto pGameBtn = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("GameBtn"));
	pGameBtn->setVisible(false);
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    //typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
    
    //auto gamebtn1 = static_cast<Button*>(pGNS_PvEAllInfoDaily->getChildByName("Button_3_0"));
    //gamebtn1->setTag(3);
    //gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
    
    TimeText = static_cast<Text*>(pGNS_PvEAllInfoDaily->getChildByName("TimeText"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));
    
    pProName = static_cast<Text*>(pGNS_PvEAllInfoDaily->getChildByName("Text_4"));
    
    GameName=static_cast<Text*>(pGNS_PvEAllInfoDaily->getChildByName("Text_3_0"));
	if (GNS_PlayerMgr::getInstance()->getGameId() > 0)
	{
		GameName->setString(GNS_PlayerMgr::getInstance()->getGameIdInfo()[GNS_PlayerMgr::getInstance()->getGameId()]);
	}

    GameType = static_cast<ListView*>(pGNS_PvEAllInfoDaily->getChildByName("GameType"));
    GameType->removeAllItems();
    
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==1) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(7);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::BtnCallBack, this));
            GameType->addChild(pbutton);

			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }
    }
    GameType->setVisible(false);

	AcceptSeverDate();
}
void GNS_PvEAllInfoDaily::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
                    this->removeAllChildrenWithCleanup(true);
					this->setVisible(false);
                    //this->getParent()->removeChild(this);
                }
                auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
                PromoterNode->setVisible(true);
                break;
            }
            case 2:
            {
					  /*pCalendar =GNS_Calendar::createCalendar();
					  pCalendar->SetCalendBgImage("Calendarbg.png");
					  std::vector<Button *> CalenderBtn;
					  pCalendar->GetTimeButton()->swap(CalenderBtn);
					  for (auto & it:CalenderBtn) {
					  it->setTag(100);
					  it->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllInfoDaily::TimeButtonCallBack, this));
					  }*/
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PvEAllInfoDaily::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 7:
            {
                GameName->setString(pButton->getTitleText());
				auto st = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
				std::map<string, int>::iterator mapGameNameIter = GNS_PlayerMgr::getInstance()->getGameNameInfo().find(st);
				if (mapGameNameIter != GNS_PlayerMgr::getInstance()->getGameNameInfo().end())
				{
                    GNS_PlayerMgr::getInstance()->getPlayProProRation()->clear();
                    CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pGetPlayPro._uplineCode = CurID;
					pGetPlayPro._gameId = mapGameNameIter->second;
                    pGetPlayPro._type=2;
                    std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                    memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO,(char*) &pGetPlayPro, sizeof(pGetPlayPro));
                    GameType->setVisible(false);
					GNS_PlayerMgr::getInstance()->setGameId(pGetPlayPro._gameId);
                }
                break;
            }
            default:
                break;
        }
    }
}
void GNS_PvEAllInfoDaily::AcceptSeverDate()
{
    DataListView->removeAllItems();
    Size bgSize;
	int index =(int)GNS_PlayerMgr::getInstance()->getPlayProProRation()->size();
	for (auto it : (*GNS_PlayerMgr::getInstance()->getPlayProProRation())) {
    //for (auto it:(*GNS_PlayerMgr::getInstance()->getGamePlayReveRes())) {
        auto Itembg = ImageView::create("line2.png");
        DataListView->addChild(Itembg);
        bgSize = Itembg->getContentSize();
        
        auto Name = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
        Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.4));
        Name->setColor(Color3B::BLACK);
        Itembg->addChild(Name);
        
        auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._lowlineRatio), GameFontName, 24);
        lineRevenueRatio->setPosition(Vec2(bgSize.width*0.45, bgSize.height*0.4));
        lineRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(lineRevenueRatio);
        
        auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._uplineRatio), GameFontName, 24);
        agentRevenueRatio->setPosition(Vec2(bgSize.width*0.8, bgSize.height*0.4));
        agentRevenueRatio->setColor(Color3B::BLACK);
        Itembg->addChild(agentRevenueRatio);
    }
    DataListView->setInnerContainerSize(Size(bgSize.width,(bgSize.height+10)*index));
}
void GNS_PvEAllInfoDaily::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        GNS_PlayerMgr::getInstance()->getPlayProProRation()->clear();
        CMD_CS_GetManToMachPlayProfitRatio pGetPlayPro;
		pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pGetPlayPro._uplineCode = CurID;
        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetPlayPro._type=2;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT_RATIO, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}
void GNS_PvEAllInfoDaily::SetProName(std::string Name)
{
	if (pProName != nullptr)
	{
		std::string strAgentCode = CCGnsOften::getUtf8_DirectAssignmentInProgram("代理商账号:");
		strAgentCode.append(SystemMgr::getInstance()->GbToUTF8(Name.c_str()));
		pProName->setString(strAgentCode);
		pProName->setVisible(true);
	}
	CurID = atoi(Name.c_str());

    //pProName->setString(StringUtils::format("代理商账号:%s",Name.c_str()));
    //CurID=atoi(Name.c_str());
}